<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha256-xejo6yLi6vGtAjcMIsY8BHdKsLg7QynVlFMzdQgUuy8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"gz1234.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.3","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"flat"},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="Object.keys()的顺序问题 将所有「合法的数组索引」按升序排序  将所有「字符串类型索引」按属性创建时间以升序排序  将所有 「Symbol 类型索引」按属性创建时间以升序排序  「合法数组索引指的是正整数，负数或者浮点数一律当做字符串处理。严格来说对象属性没有数字类型的，无论是数字还是字符串，都会被当做字符串来处理。」   Object.keysObject.keys JSON.str">
<meta property="og:type" content="article">
<meta property="og:title" content="代码中注意问题">
<meta property="og:url" content="https://gz1234.gitee.io/2022/07/10/%E4%BB%A3%E7%A0%81%E4%B8%AD%E6%B3%A8%E6%84%8F%E9%97%AE%E9%A2%98/index.html">
<meta property="og:site_name" content="郭泽">
<meta property="og:description" content="Object.keys()的顺序问题 将所有「合法的数组索引」按升序排序  将所有「字符串类型索引」按属性创建时间以升序排序  将所有 「Symbol 类型索引」按属性创建时间以升序排序  「合法数组索引指的是正整数，负数或者浮点数一律当做字符串处理。严格来说对象属性没有数字类型的，无论是数字还是字符串，都会被当做字符串来处理。」   Object.keysObject.keys JSON.str">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-07-09T16:55:43.000Z">
<meta property="article:modified_time" content="2023-08-11T09:52:59.853Z">
<meta property="article:author" content="郭泽">
<meta property="article:tag" content="代码中注意问题">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gz1234.gitee.io/2022/07/10/%E4%BB%A3%E7%A0%81%E4%B8%AD%E6%B3%A8%E6%84%8F%E9%97%AE%E9%A2%98/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://gz1234.gitee.io/2022/07/10/%E4%BB%A3%E7%A0%81%E4%B8%AD%E6%B3%A8%E6%84%8F%E9%97%AE%E9%A2%98/","path":"2022/07/10/代码中注意问题/","title":"代码中注意问题"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>代码中注意问题 | 郭泽</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">郭泽</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Object-keys-%E7%9A%84%E9%A1%BA%E5%BA%8F%E9%97%AE%E9%A2%98"><span class="nav-number">1.</span> <span class="nav-text">Object.keys()的顺序问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JSON-stringify-%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E4%B8%A2%E5%A4%B1%E9%97%AE%E9%A2%98"><span class="nav-number">2.</span> <span class="nav-text">JSON.stringify 使用数据丢失问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#if-else-%E8%AF%AD%E5%8F%A5%E9%87%8C%E4%B8%8D%E8%83%BD%E7%94%A8%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0"><span class="nav-number">3.</span> <span class="nav-text">if else 语句里不能用函数声明定义函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#js-toFixed%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%E5%87%BA%E7%8E%B0%E7%9A%84%E7%B2%BE%E5%BA%A6%E9%97%AE%E9%A2%98"><span class="nav-number">4.</span> <span class="nav-text">js toFixed四舍五入出现的精度问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#forEach%E4%B8%AD%E4%B8%8D%E5%87%BA%E7%8E%B0%E5%BC%82%E6%AD%A5%E4%BB%A3%E7%A0%81"><span class="nav-number">5.</span> <span class="nav-text">forEach中不出现异步代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0%E4%B8%8D%E6%98%AF%E4%B8%87%E8%83%BD%E7%9A%84-4%E4%B8%AD%E6%83%85%E5%86%B5%E7%A6%81%E6%AD%A2%E4%BD%BF%E7%94%A8"><span class="nav-number">6.</span> <span class="nav-text">箭头函数不是万能的,4中情况禁止使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue-%E7%BB%84%E4%BB%B6methods%E7%9A%84%E9%98%B2%E6%8A%96%E5%87%BD%E6%95%B0-%E4%B8%8D%E8%83%BD%E5%A4%9A%E5%B0%91%E6%AC%A1%E8%B0%83%E7%94%A8%E8%AF%A5%E7%BB%84%E4%BB%B6%EF%BC%8C%E5%90%A6%E5%88%99%E8%A6%81%E5%B0%86%E9%98%B2%E6%8A%96%E5%87%BD%E6%95%B0%E7%BB%91%E5%AE%9A%E5%9C%A8data-%E5%B1%9E%E6%80%A7%E4%B8%8A"><span class="nav-number">7.</span> <span class="nav-text">vue 组件methods的防抖函数, 不能多少次调用该组件，否则要将防抖函数绑定在data 属性上</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JavaScript-%E5%BC%82%E6%AD%A5%E4%BB%A3%E7%A0%81%E7%9A%84%E5%87%A0%E4%B8%AA%E6%8E%A8%E8%8D%90%E5%81%9A%E6%B3%95"><span class="nav-number">8.</span> <span class="nav-text">JavaScript 异步代码的几个推荐做法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#async%E3%80%81await-%E6%8D%95%E8%8E%B7%E9%94%99%E8%AF%AF-await-to-js"><span class="nav-number">9.</span> <span class="nav-text">async、await 捕获错误 (await-to-js)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E9%97%AD%E5%8C%85%E6%BC%8F%E6%B4%9E%E9%97%AE%E9%A2%98"><span class="nav-number">10.</span> <span class="nav-text">代码闭包漏洞问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%98%E5%9C%A8%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-number">10.1.</span> <span class="nav-text">存在的问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95"><span class="nav-number">10.2.</span> <span class="nav-text">解决方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#flex-%E5%B8%83%E5%B1%80%E5%9D%87%E5%8C%80%E5%88%86%E5%B8%83%E6%B3%A8%E6%84%8F"><span class="nav-number">11.</span> <span class="nav-text">flex 布局均匀分布注意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%94%A8-Depcheck-%E5%8E%BB%E9%99%A4%E9%A1%B9%E7%9B%AE%E6%B2%A1%E6%9C%89%E7%94%A8%E5%88%B0%E7%9A%84%E4%BE%9D%E8%B5%96"><span class="nav-number">12.</span> <span class="nav-text">用 Depcheck 去除项目没有用到的依赖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AD%A3%E5%88%99%E5%8C%B9%E9%85%8D-g-%E5%85%A8%E5%B1%80%E4%BF%AE%E9%A5%B0%E7%AC%A6-lastIndex%E8%B5%B7%E4%BD%9C%E7%94%A8"><span class="nav-number">13.</span> <span class="nav-text">正则匹配 g 全局修饰符 lastIndex起作用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-%E5%A4%A7%E5%B0%8F%E5%86%99%E9%97%AE%E9%A2%98"><span class="nav-number">14.</span> <span class="nav-text">git 大小写问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue2-%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%B1%95%E7%A4%BA%E6%80%A7%E7%9A%84-%E4%BD%BF%E7%94%A8Object-freeze-%E5%86%BB%E7%BB%93%E5%AF%B9%E8%B1%A1%E8%BE%BE%E5%88%B0%E4%BC%98%E5%8C%96%E7%9B%AE%E7%9A%84"><span class="nav-number">15.</span> <span class="nav-text">vue2 大数据展示性的 使用Object.freeze() 冻结对象达到优化目的</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%9A%E7%AA%97%E5%8F%A3%E4%B9%8B%E9%97%B4sessionStorage"><span class="nav-number">16.</span> <span class="nav-text">多窗口之间sessionStorage</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#BroadcastChannel-%E4%B8%80%E4%B8%AA%E9%A2%91%E9%81%93-%E5%85%81%E8%AE%B8%E5%90%8C%E6%BA%90%E7%9A%84%E4%B8%8D%E5%90%8C%E6%B5%8F%E8%A7%88%E5%99%A8%E7%AA%97%E5%8F%A3%EF%BC%8CTab-%E9%A1%B5%EF%BC%8Cframe-%E6%88%96%E8%80%85-iframe-%E4%B8%8B%E7%9A%84%E4%B8%8D%E5%90%8C%E6%96%87%E6%A1%A3%E4%B9%8B%E9%97%B4%E7%9B%B8%E4%BA%92%E9%80%9A%E4%BF%A1"><span class="nav-number">17.</span> <span class="nav-text">BroadcastChannel 一个频道 允许同源的不同浏览器窗口，Tab 页，frame 或者 iframe 下的不同文档之间相互通信</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#watchEffect-%E5%8F%AA%E8%83%BD%E6%94%B6%E9%9B%86%E5%90%8C%E6%AD%A5%E4%BB%A3%E7%A0%81%E7%9A%84%E4%BE%9D%E8%B5%96"><span class="nav-number">18.</span> <span class="nav-text">watchEffect  只能收集同步代码的依赖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A1%A8%E5%8D%95%E7%9A%84%E5%B0%81%E8%A3%85-%E6%95%B0%E6%8D%AE%E4%BB%A3%E7%90%86"><span class="nav-number">19.</span> <span class="nav-text">表单的封装 数据代理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E7%AB%AF%E6%8D%95%E8%8E%B7%E5%BC%82%E5%B8%B8"><span class="nav-number">20.</span> <span class="nav-text">前端捕获异常</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#try-catch"><span class="nav-number">20.1.</span> <span class="nav-text">try-catch</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#window-addEventListener-%E2%80%98error%E2%80%99-%E5%87%BD%E6%95%B0%E6%8D%95%E8%8E%B7"><span class="nav-number">20.2.</span> <span class="nav-text">window.addEventListener(‘error’,) 函数捕获</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#window-onerror-%E5%AF%B9-JavaScript-%E8%BF%90%E8%A1%8C%E9%94%99%E8%AF%AF%E6%97%B6%E8%BF%9B%E8%A1%8C%E6%8D%95%E8%8E%B7"><span class="nav-number">20.3.</span> <span class="nav-text">window.onerror 对 JavaScript 运行错误时进行捕获</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Promise-catch%EF%BC%8C-unhandledrejection-%E6%8D%95%E8%8E%B7%E5%BC%82%E6%AD%A5%E9%94%99%E8%AF%AF"><span class="nav-number">20.4.</span> <span class="nav-text">　Promise catch，　unhandledrejection　　捕获异步错误</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vue-errorHandler"><span class="nav-number">20.5.</span> <span class="nav-text">vue errorHandler</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#react-%E5%BC%82%E5%B8%B8%E6%8D%95%E8%8E%B7"><span class="nav-number">20.6.</span> <span class="nav-text">react 异常捕获</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89%E7%BA%A7%E8%B7%AF%E7%94%B1-keep-alive-%E5%A4%B1%E6%95%88"><span class="nav-number">21.</span> <span class="nav-text">三级路由 keep-alive 失效</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keep-alive-%E6%B3%A8%E6%84%8F"><span class="nav-number">22.</span> <span class="nav-text">keep-alive 注意</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">郭泽</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">51</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gz1234.gitee.io/2022/07/10/%E4%BB%A3%E7%A0%81%E4%B8%AD%E6%B3%A8%E6%84%8F%E9%97%AE%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="郭泽">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郭泽">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="代码中注意问题 | 郭泽">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          代码中注意问题
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-07-10 00:55:43" itemprop="dateCreated datePublished" datetime="2022-07-10T00:55:43+08:00">2022-07-10</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-08-11 17:52:59" itemprop="dateModified" datetime="2023-08-11T17:52:59+08:00">2023-08-11</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E4%BB%A3%E7%A0%81%E4%B8%AD%E6%B3%A8%E6%84%8F%E9%97%AE%E9%A2%98/" itemprop="url" rel="index"><span itemprop="name">代码中注意问题</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="Object-keys-的顺序问题"><a href="#Object-keys-的顺序问题" class="headerlink" title="Object.keys()的顺序问题"></a>Object.keys()的顺序问题</h2><ul>
<li><p>将所有「合法的数组索引」按升序排序</p>
</li>
<li><p>将所有「字符串类型索引」按属性创建时间以升序排序</p>
</li>
<li><p>将所有 「Symbol 类型索引」按属性创建时间以升序排序</p>
</li>
<li><p>「<code>合法数组索引指的是正整数，负数或者浮点数一律当做字符串处理。严格来说对象属性没有数字类型的，无论是数字还是字符串，都会被当做字符串来处理。</code>」</p>
</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq846294282/article/details/118719889">Object.keys</a><br><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/2_yFU6iS9ALOXgM--LyaEA">Object.keys</a></p>
<h2 id="JSON-stringify-使用数据丢失问题"><a href="#JSON-stringify-使用数据丢失问题" class="headerlink" title="JSON.stringify 使用数据丢失问题"></a>JSON.stringify 使用数据丢失问题</h2><p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/rTuiUtpTB6iOqCNb7yv0zQ">JSON.stringify</a></p>
<h2 id="if-else-语句里不能用函数声明定义函数"><a href="#if-else-语句里不能用函数声明定义函数" class="headerlink" title="if else 语句里不能用函数声明定义函数"></a>if else 语句里不能用函数声明定义函数</h2><h2 id="js-toFixed四舍五入出现的精度问题"><a href="#js-toFixed四舍五入出现的精度问题" class="headerlink" title="js toFixed四舍五入出现的精度问题"></a>js toFixed四舍五入出现的精度问题</h2><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_38128179/article/details/89181805?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1-89181805-blog-123878883.pc_relevant_aa&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1-89181805-blog-123878883.pc_relevant_aa&utm_relevant_index=2">解决JS浮点数运算结果不精确的Bug</a><br> 解决办法的类库 <a target="_blank" rel="noopener" href="https://mathjs.org/">Math.js</a> <a target="_blank" rel="noopener" href="http://mikemcl.github.io/big.js/">big.js</a> <a target="_blank" rel="noopener" href="http://mikemcl.github.io/decimal.js/">decimal.js</a></p>
<h2 id="forEach中不出现异步代码"><a href="#forEach中不出现异步代码" class="headerlink" title="forEach中不出现异步代码"></a>forEach中不出现异步代码</h2><a id="more"></a>

<h2 id="箭头函数不是万能的-4中情况禁止使用"><a href="#箭头函数不是万能的-4中情况禁止使用" class="headerlink" title="箭头函数不是万能的,4中情况禁止使用"></a>箭头函数不是万能的,4中情况禁止使用</h2><ul>
<li>请不要在构造函数中使用箭头函数</li>
<li>请不要在点击事件中操作this</li>
<li>请不要在对象的方法中使用箭头函数</li>
<li>请不要在原型链中使用箭头函数</li>
</ul>
<h2 id="vue-组件methods的防抖函数-不能多少次调用该组件，否则要将防抖函数绑定在data-属性上"><a href="#vue-组件methods的防抖函数-不能多少次调用该组件，否则要将防抖函数绑定在data-属性上" class="headerlink" title="vue 组件methods的防抖函数, 不能多少次调用该组件，否则要将防抖函数绑定在data 属性上"></a>vue 组件methods的防抖函数, 不能多少次调用该组件，否则要将防抖函数绑定在data 属性上</h2><h2 id="JavaScript-异步代码的几个推荐做法"><a href="#JavaScript-异步代码的几个推荐做法" class="headerlink" title="JavaScript 异步代码的几个推荐做法"></a>JavaScript 异步代码的几个推荐做法</h2><p> [异步代码的几个推荐做法] <a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/1Py2vPwjjqw17rn-uBfJ7g">https://mp.weixin.qq.com/s/1Py2vPwjjqw17rn-uBfJ7g</a></p>
<h2 id="async、await-捕获错误-await-to-js"><a href="#async、await-捕获错误-await-to-js" class="headerlink" title="async、await 捕获错误 (await-to-js)"></a>async、await 捕获错误 (await-to-js)</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> to <span class="keyword">from</span> <span class="string">&#x27;await-to-js&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> [err, user] = <span class="keyword">await</span> to(UserModel.findById(<span class="number">1</span>));</span><br><span class="line">  <span class="keyword">if</span> (!user) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;User not found&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://github.com/scopsy/await-to-js">await-to-js</a></p>
<h2 id="代码闭包漏洞问题"><a href="#代码闭包漏洞问题" class="headerlink" title="代码闭包漏洞问题"></a>代码闭包漏洞问题</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在不修改下面代码的情况下，修改obj的内容</span></span><br><span class="line"><span class="keyword">var</span> o = (<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> obj = &#123;</span><br><span class="line">        a:<span class="number">1</span>,</span><br><span class="line">        b:<span class="number">2</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        get :<span class="function">(<span class="params">n</span>)=&gt;</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> obj[n]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="存在的问题"><a href="#存在的问题" class="headerlink" title="存在的问题"></a>存在的问题</h3><p>闭包模式。屏蔽掉obj本身。只能访问闭包返回的数据而不能去修改数据源本身，但是他的数据源是一个对象，这就会出现一个漏洞！！！！，而上面的代码就会出现一个致命的漏洞</p>
<ul>
<li><p>那我能不能去访问到原型valueOf，暂时说想通过valueOf（）来获取这个对象本身。<br>JavaScript 中的 valueOf() 方法用于返回指定对象的原始值，若对象没有原始值，则将返回对象本身。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(o.get(<span class="string">&quot;valueOf&quot;</span>));</span><br><span class="line"><span class="comment">//[Function: valueOf]</span></span><br><span class="line"><span class="keyword">let</span> obj2 = o.get(<span class="string">&quot;valueOf&quot;</span>)()</span><br><span class="line"><span class="comment">//TypeError: Cannot convert undefined or null to object</span></span><br></pre></td></tr></table></figure>

<p>可以看的出来，报错，这是因为valueOf函数的this指向发生了错误</p>
</li>
<li><p>虽然提供了一个函数进入传参，但是传入的权限很大，就会有很大的问题。<br>有没有一种方法，直接访问对象的属性的时候通过一个函数来获取，这时候就是构造器属性。<br>给对象的原型设置一个访问器属性</p>
  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">  <span class="built_in">Object</span>.defineProperty(<span class="built_in">Object</span>.prototype,<span class="string">&quot;abc&quot;</span>,&#123;</span><br><span class="line">    <span class="function"><span class="title">get</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(o.get(<span class="string">&quot;abc&quot;</span>));<span class="comment">//&#123; a: 1, b: 2 &#125;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> obj = o.get(<span class="string">&quot;abc&quot;</span>)</span><br><span class="line">obj.a=<span class="string">&#x27;hjkasd&#x27;</span></span><br><span class="line">obj.b=<span class="string">&#x27;12345&#x27;</span></span><br><span class="line">obj.c=<span class="string">&#x27;勇敢牛牛，不怕困难&#x27;</span></span><br><span class="line"><span class="built_in">console</span>.log(obj);<span class="comment">//&#123; a: &#x27;hjkasd&#x27;, b: &#x27;12345&#x27;, c: &#x27;勇敢牛牛，不怕困难&#x27; &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="解决方法"><a href="#解决方法" class="headerlink" title="解决方法"></a>解决方法</h3><ul>
<li><p>判断访问的属性是否来自对象本身。从而返回不同的值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> o = (<span class="function">()=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> obj = &#123;</span><br><span class="line">      a:<span class="number">1</span>,</span><br><span class="line">      b:<span class="number">2</span>,</span><br><span class="line">  &#125;;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">      get :<span class="function">(<span class="params">n</span>)=&gt;</span>&#123;</span><br><span class="line">          <span class="function"><span class="title">if</span>(<span class="params">!obj.hasOwnProperty(n)</span>)</span>&#123;</span><br><span class="line">              <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;n&#125;</span>在此对象本身不存在`</span></span><br><span class="line">          &#125;</span><br><span class="line">          <span class="keyword">return</span> obj[n]</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">&#125;)()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>直接设置这个对象的原型为null，没有原型链，那我上面的逻辑就行不通，自然就保护了数据的隐私性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> o = (<span class="function">()=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">let</span> obj = &#123;</span><br><span class="line">      a:<span class="number">1</span>,</span><br><span class="line">      b:<span class="number">2</span>,</span><br><span class="line">  &#125;;</span><br><span class="line">  <span class="built_in">Object</span>.setPrototypeOf(obj,<span class="literal">null</span>)</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">      get :<span class="function">(<span class="params">n</span>)=&gt;</span>&#123;</span><br><span class="line">          <span class="keyword">return</span> obj[n]</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">&#125;)()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="flex-布局均匀分布注意"><a href="#flex-布局均匀分布注意" class="headerlink" title="flex 布局均匀分布注意"></a>flex 布局均匀分布注意</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.container</span>&#123;</span><br><span class="line">  <span class="attribute">display</span>: flex;</span><br><span class="line">  .item&#123;</span><br><span class="line">    <span class="selector-tag">flex-grow</span>: 1;</span><br><span class="line">    // flex-basis: 0;不设置该属性时 当元素内有内容会造成不均匀分布</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="用-Depcheck-去除项目没有用到的依赖"><a href="#用-Depcheck-去除项目没有用到的依赖" class="headerlink" title="用 Depcheck 去除项目没有用到的依赖"></a>用 Depcheck 去除项目没有用到的依赖</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm install -g depcheck</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_39111622/article/details/122072187">depcheck</a></p>
<h2 id="正则匹配-g-全局修饰符-lastIndex起作用"><a href="#正则匹配-g-全局修饰符-lastIndex起作用" class="headerlink" title="正则匹配 g 全局修饰符 lastIndex起作用"></a>正则匹配 g 全局修饰符 lastIndex起作用</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#规则如下</span><br><span class="line"><span class="number">1.</span>初始状态lastIndex为<span class="number">0</span></span><br><span class="line"><span class="number">2.</span>在进行匹配校验的过程中，若成功匹配，那么lastIndex就会等于符合规则的字符串的最后一个下标再+<span class="number">1</span></span><br><span class="line"><span class="number">3.</span>匹配失败，lastIndex会置为<span class="number">0</span></span><br><span class="line"><span class="number">4.</span>如果继续用这个正则去匹配的话，就会从这个lastIndex开始</span><br></pre></td></tr></table></figure>

<h2 id="git-大小写问题"><a href="#git-大小写问题" class="headerlink" title="git 大小写问题"></a>git 大小写问题</h2><p>默认情况下，git是会忽略文件名大小写的;<br>执行git config –global core.ignorecase false，全局设置 大小写敏感;<br> <code>多人合作一个文件 通知统一处理</code></p>
<h2 id="vue2-大数据展示性的-使用Object-freeze-冻结对象达到优化目的"><a href="#vue2-大数据展示性的-使用Object-freeze-冻结对象达到优化目的" class="headerlink" title="vue2 大数据展示性的 使用Object.freeze() 冻结对象达到优化目的"></a>vue2 大数据展示性的 使用Object.freeze() 冻结对象达到优化目的</h2><h2 id="多窗口之间sessionStorage"><a href="#多窗口之间sessionStorage" class="headerlink" title="多窗口之间sessionStorage"></a>多窗口之间sessionStorage</h2><p>多窗口之间sessionStorage不可以共享状态！！！但是在某些特定场景下(在本页面中以新页签或窗口打开的同源页面)新开的页面会复制之前页面的sessionStorage！！<br>其实不仅window.open(“同源页面”)这种方式新开的页面会复制之前的sessionStorage，通过a标签(a标签需要加入rel=“opener” 而才能像window.open)新开的页面同样也会</p>
<h2 id="BroadcastChannel-一个频道-允许同源的不同浏览器窗口，Tab-页，frame-或者-iframe-下的不同文档之间相互通信"><a href="#BroadcastChannel-一个频道-允许同源的不同浏览器窗口，Tab-页，frame-或者-iframe-下的不同文档之间相互通信" class="headerlink" title="BroadcastChannel 一个频道 允许同源的不同浏览器窗口，Tab 页，frame 或者 iframe 下的不同文档之间相互通信"></a>BroadcastChannel 一个频道 允许同源的不同浏览器窗口，Tab 页，frame 或者 iframe 下的不同文档之间相互通信</h2><p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/BroadcastChannel">BroadcastChannel</a></p>
<h2 id="watchEffect-只能收集同步代码的依赖"><a href="#watchEffect-只能收集同步代码的依赖" class="headerlink" title="watchEffect  只能收集同步代码的依赖"></a>watchEffect  只能收集同步代码的依赖</h2><h2 id="表单的封装-数据代理"><a href="#表单的封装-数据代理" class="headerlink" title="表单的封装 数据代理"></a>表单的封装 数据代理</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">useVModel</span>(<span class="params">prop,propName,emit</span>)</span>&#123;</span><br><span class="line"><span class="keyword">return</span> computed(<span class="function">() =&gt;</span>&#123;</span><br><span class="line">  <span class="function"><span class="title">get</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Proxy</span>(prop[propName],&#123;</span><br><span class="line">      <span class="function"><span class="title">set</span>(<span class="params">obj,name,value</span>)</span>&#123;</span><br><span class="line">        emit(<span class="string">&#x27;update:&#x27;</span>+ propName,&#123;</span><br><span class="line">          ...obj,</span><br><span class="line">          [name]: value</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">set</span>(<span class="params">newVal</span>)</span>&#123;</span><br><span class="line">    emit(<span class="string">&#x27;update:&#x27;</span>+ propName,newVal)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> props =  defineProps(&#123;</span><br><span class="line">  modelValue:&#123;</span><br><span class="line">    type: <span class="built_in">Object</span>,</span><br><span class="line">    required: <span class="literal">true</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> emit =  defineEmit([<span class="string">&#x27;update:modelValue&#x27;</span>]);</span><br><span class="line"><span class="keyword">let</span> model = useVModel(props,<span class="string">&quot;modelValue&quot;</span>,emit);</span><br><span class="line"> </span><br></pre></td></tr></table></figure>

<h2 id="前端捕获异常"><a href="#前端捕获异常" class="headerlink" title="前端捕获异常"></a>前端捕获异常</h2><h3 id="try-catch"><a href="#try-catch" class="headerlink" title="try-catch"></a>try-catch</h3><p>使用 try-catch 对同步代码运行异常进行捕获</p>
<h3 id="window-addEventListener-‘error’-函数捕获"><a href="#window-addEventListener-‘error’-函数捕获" class="headerlink" title="window.addEventListener(‘error’,) 函数捕获"></a>window.addEventListener(‘error’,) 函数捕获</h3><p>对静态资源加载异常与接口请求异常进行捕获</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;scritp&gt;</span><br><span class="line">window.addEventListener(&#39;error&#39;, (error) &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;捕获到异常：&#39;, error);</span><br><span class="line">&#125;, true)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line">&lt;img src&#x3D;&quot;..&#x2F;..&#x2F;assets&#x2F;test.png&quot;&gt;</span><br></pre></td></tr></table></figure>

<h3 id="window-onerror-对-JavaScript-运行错误时进行捕获"><a href="#window-onerror-对-JavaScript-运行错误时进行捕获" class="headerlink" title="window.onerror 对 JavaScript 运行错误时进行捕获"></a>window.onerror 对 JavaScript 运行错误时进行捕获</h3><ul>
<li><p>window.onerror 最好写在所有 JavaScript 脚本前面，否则有可能捕获不到异常；</p>
</li>
<li><p>window.onerror 无法捕获语法异常、静态资源异常、接口请求异常；</p>
</li>
<li><p>window.onerror 主要用来捕获意料之外的异常，而 try-catch 则是用来捕获可预见的异常。</p>
</li>
</ul>
<h3 id="Promise-catch，-unhandledrejection-捕获异步错误"><a href="#Promise-catch，-unhandledrejection-捕获异步错误" class="headerlink" title="　Promise catch，　unhandledrejection　　捕获异步错误"></a>　Promise catch，　unhandledrejection　　捕获异步错误</h3><p>写 Promise 会漏掉 catch，所以建议在全局增加一个对 unhandledrejection 的监听，用来全局监听 Uncaught Promise Error。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">&quot;unhandledrejection&quot;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;</span><br><span class="line">  e.preventDefault()</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;捕获到异常：&#x27;</span>, e);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve,reject</span>) =&gt;</span> &#123;</span><br><span class="line">  reject(<span class="string">&quot;执行失败！&quot;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="vue-errorHandler"><a href="#vue-errorHandler" class="headerlink" title="vue errorHandler"></a>vue errorHandler</h3><p> errorHandler 对 vue 组件中所抛错误的捕捉与处理</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"> Vue.config.errorHandler = <span class="function">(<span class="params">err, vm, info</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.error(err);</span><br><span class="line">  <span class="built_in">console</span>.error(vm);</span><br><span class="line">  <span class="built_in">console</span>.error(info);</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="comment">// 某个组件的 mounted</span></span><br><span class="line"><span class="keyword">const</span> error = <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;test error&#x27;</span>);</span><br><span class="line">error.code = -<span class="number">1</span>;</span><br><span class="line"><span class="keyword">throw</span> error;</span><br></pre></td></tr></table></figure>

<h3 id="react-异常捕获"><a href="#react-异常捕获" class="headerlink" title="react 异常捕获"></a>react 异常捕获</h3><p>内置函数 componentDidCatch，使用它可以非常简单的获取到 react 下的错误信息,也可以使用错误边界 error boundary</p>
<h2 id="三级路由-keep-alive-失效"><a href="#三级路由-keep-alive-失效" class="headerlink" title="三级路由 keep-alive 失效"></a>三级路由 keep-alive 失效</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 解决三级路由 keep-alive 失效的问题</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 递归处理多余的 ParentView : &lt;router-view&gt;，</span></span><br><span class="line"><span class="comment"> * 让需要访问的组件保持在第一层 index : &lt;router-view&gt; 之下</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="variable">to</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">handleKeepAlive</span>(<span class="params">to</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (to.matched &amp;&amp; to.matched.length &gt; <span class="number">2</span>) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; to.matched.length; i++) &#123;</span><br><span class="line">      <span class="keyword">const</span> element = to.matched[i];</span><br><span class="line">      <span class="keyword">if</span> (element.components.default.name.indexOf(<span class="string">&quot;ParentView&quot;</span>) !== -<span class="number">1</span>) &#123;</span><br><span class="line">        to.matched.splice(i, <span class="number">1</span>);</span><br><span class="line">        handleKeepAlive(to);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="keep-alive-注意"><a href="#keep-alive-注意" class="headerlink" title="keep-alive 注意"></a>keep-alive 注意</h2><p> include/exclude 值是组件中的 name 命名，而不是路由中的组件 name 命名</p>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E4%BB%A3%E7%A0%81%E4%B8%AD%E6%B3%A8%E6%84%8F%E9%97%AE%E9%A2%98/" rel="tag"># 代码中注意问题</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/Buffer/" rel="prev" title="Buffer">
                  <i class="fa fa-chevron-left"></i> Buffer
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/07/27/%E7%AE%97%E6%B3%95%E6%80%9D%E6%83%B3/" rel="next" title="算法思想">
                  算法思想 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">郭泽</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.8/pdfobject.min.js","integrity":"sha256-tu9j5pBilBQrWSDePOOajCUdz6hWsid/lBNzK4KgEPM="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>

  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.1.3/mermaid.min.js","integrity":"sha256-TIYL00Rhw/8WaoUhYTLX9SKIEFdXxg+yMWSLVUbhiLg="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>

  <script src="/js/third-party/fancybox.js"></script>


  





</body>
</html>
